﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using smartphone.App_Code;

namespace smartphone.UserControls
{
    public partial class AdminHomeSlideShow : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "GetHomeSlideImage";

            DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
            GridViewHomeImage.DataSource = table;
            GridViewHomeImage.DataBind();
        }

        protected void GridViewHomeImage_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridViewHomeImage.DataKeys[e.RowIndex].Value.ToString();
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "DeleteHomeImage";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maHtt";
            param.DbType = DbType.Int32;
            param.Value = Int32.Parse(id);
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);
            GridViewHomeImage.EditIndex = -1;
            //LabelInform.Text = (result != -1) ? "Xóa thành công" : "Xóa không thành công";

            BindGrid();
        }

        protected void GridViewHomeImage_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = GridViewHomeImage.DataKeys[e.RowIndex].Value.ToString();
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "UpdateHomeImage";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maHtt";
            param.DbType = DbType.Int32;
            param.Value = Int32.Parse(id);
            comm.Parameters.Add(param);

            string newUrl = ((TextBox)GridViewHomeImage.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            param = comm.CreateParameter();
            param.ParameterName = "@Url";
            param.DbType = DbType.String;
            param.Value = newUrl;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);
            GridViewHomeImage.EditIndex = -1;
            //LabelInform.Text = (result != -1) ? "Cập nhật đường dẫn thành công" : "Cập nhật đường dẫn thật bại";

            BindGrid();
        }

        protected void GridViewHomeImage_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewHomeImage.EditIndex = e.NewEditIndex;
            //LabelInform.Text = "Sủa dòng số " + e.NewEditIndex.ToString();

            BindGrid();
        }

        protected void GridViewHomeImage_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewHomeImage.EditIndex = -1;
            //LabelInform.Text = "Hủy sửa";
            BindGrid();
        }

        protected void BtCreateHomeImage_Click(object sender, EventArgs e)
        {
            string url = TxtUrl.Text;
            string newImgName = FileUploadHomeImage.FileName;
            string location = Server.MapPath("./ProductImages/") + newImgName;
            FileUploadHomeImage.SaveAs(location);
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "CreateHomeImage";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@HttUrl";
            param.DbType = DbType.String;
            param.Value = newImgName;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@Url";
            param.DbType = DbType.String;
            param.Value = url;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);
            GridViewHomeImage.EditIndex = -1;
            //LabelInform.Text = (result != -1) ? "Tạo thành công" : "Tạo không thành công";

            BindGrid();
        }

    }
}